﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        private IHttpClientFactory _httpClientFactory;

        public HomeController(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public IActionResult Index()
        {
            return View();
        }

        public JsonResult CreateConnectionRequest(string nickname)
        {
            var res = new ConnectionRequest(nickname + "123", nickname, ToUnixTimestamp(DateTime.Now));

            return Json(res);
        }

        public async Task<ContentResult> PushMessageTest()
        {
            var client = _httpClientFactory.CreateClient("auok");

            long timestamp = ToUnixTimestamp(DateTime.Now);
            string sign = ConnectionRequest.GetMd5Hash("5F9DF7CCEDF5F0F58D9D8F9C9C950F10" + timestamp);

            client.DefaultRequestHeaders.Add("timestamp", timestamp.ToString());
            client.DefaultRequestHeaders.Add("sign", sign);

            var res = await client.PostAsJsonAsync("http://127.0.0.1:8080/api/server/pushmessage", new
            {
                fromUserId = "aaa123",
                toUserId = "bbb123",
                content = "message test",
                onlyOnline = true
            });

            string content = await res.Content.ReadAsStringAsync();

            return Content(content);
        }

        public static long ToUnixTimestamp(DateTime target)
        {
            return Convert.ToInt64((TimeZoneInfo.ConvertTimeToUtc(target) -
                                    new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds);
        }
    }

    public class ConnectionRequest
    {
        public string sign;


        public string userId;


        public string nickName;


        public long timestamp;

        public ConnectionRequest(string userId, string nickName, long timestamp)
        {
            this.userId = userId;
            this.nickName = nickName;
            this.timestamp = timestamp;

            this.sign = GetMd5Hash("5F9DF7CCEDF5F0F58D9D8F9C9C950F10" + timestamp);
        }

        #region MD5加密

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetMd5Hash(string input, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }


            MD5 myMD5 = new MD5CryptoServiceProvider();
            byte[] signed = myMD5.ComputeHash(encoding.GetBytes(input));
            string signResult = byte2mac(signed);
            return signResult.ToUpper();
        }

        //MD5加密方法
        private static string byte2mac(byte[] signed)
        {
            StringBuilder EnText = new StringBuilder();
            foreach (byte Byte in signed)
            {
                EnText.AppendFormat("{0:x2}", Byte);
            }

            return EnText.ToString();
        }

        #endregion
    }
}